using UnityEngine;
using System.Collections;

public class backClass : MonoBehaviour {
	
	//shooting
	public float shootCooldown;
	public float baseShootCooldown = 25f;
	private float shootTimer = 0;
    public OTSprite bullet;     
	
	private globalVarsClass gv;
    public OTSprite enemy;  
	public OTSprite shield;  
	public OTSprite blastWaveShield;
    OTSprite gun;  
	
	
	//game pause
	public bool gameStopped = false;
	
	//spawn
	private float spawnTimer = 0;
	public float spawnDelay = 10.5f;          
	
	
	// shield
	private float shieldRange;      
	
	//Инициализация
	void Start () {	
		shootCooldown = baseShootCooldown;
		shieldRange = (shield.size.x)/2; 
		
        gun = OT.ObjectByName("Player") as OTSprite;
		gv = GameObject.Find("GlobalVariables").GetComponent<globalVarsClass>();
		blastWaveShield = GameObject.Find("BlastWaveShield").GetComponent<OTSprite>();
	}
	//пауза
	public void setPause(bool pause){
		if (pause){
			gameStopped = true;
		}else{
			gameStopped = false;
		}
	}
	
	//Получение урона игроком
	public bool takeDamage(float damageAmount, OTSprite shooter){
		if(gv.shieldHP > damageAmount){
			//gv.shieldHP -= damageAmount;
			gv.shield.getDamage( damageAmount, shooter);
			damageAmount = 0;
		}else{
			damageAmount -= gv.shieldHP;
			gv.shieldHP = 0;
		}
		gv.playerHP -= damageAmount;
		if(gv.playerHP < 0){
			return true;
		}else{
			return false;
		}
	}
	// Update is called once per frame
	private void Update () {
		
		//pause
		if(gameStopped) return;
		
		//spawn enemies
		spawnTimer += Time.deltaTime;
		if(spawnTimer > spawnDelay){
			spawnTimer = 0;
			GameObject.Find("MonsterSpawner").GetComponent<monsterSpawnerClass>().spawn(enemy);
		}
		//rotate gun
		gun.RotateTowards(OT.view.mouseWorldPosition);
		
		
		//enemies` bullets` moviment and effects
		for(int i = 0; i < gv.enemyBulletList.Count; i++){
			if(gv.enemyBulletList[i].GetComponent<bulletClass>().moveMe()){
				gv.removeEnemyBullet(i);
				i--;
				continue;
			}
			if (Vector2.Distance(gv.enemyBulletList[i].position, gun.position) < shieldRange)                         // added
			{
				this.takeDamage(2, gv.enemyBulletList[i].GetComponent<bulletClass>().shooter);
				gv.removeEnemyBullet(i);
				i--;
			}
		}
		
		//player`s bullets` moviment and effects
		for(int i = 0; i < gv.bulletList.Count; i++){
			bulletClass bc = gv.bulletList[i].GetComponent<bulletClass>();
			if(bc.moveMe()){
				gv.removeBullet(i);
			}else if(bc.hitEnemy(bc.checkHit())){
				gv.removeBullet(i);
			}
		}
		
		//enemies` moviment and actions
		for(int j = 0; j < gv.MobList.Count; j++){	
			if (Vector2.Distance(gv.MobList[j].position, blastWaveShield.position) > blastWaveShield.size.x/2)													//explosion wave
				gv.MobList[j].GetComponent<badGuyClass>().moveMe();
			else 
				gv.MobList[j].GetComponent<badGuyClass>().blastMe();
			
			if (Vector2.Distance(gv.MobList[j].position, gun.position) < shieldRange)                         // added
			{
				gv.removeEnemy(j);
				j--;
				this.takeDamage(10, null);
			}else if(gv.globalBonus == 2 && Vector2.Distance(gv.MobList[j].position, gun.position) < 8*shieldRange){
				if(gv.MobList[j].GetComponent<badGuyClass>().takeDamage(gv.MobList[j].GetComponent<badGuyClass>().health/500, 1)){
					gv.removeEnemy(j);
					j--;
				}
			}
		}
		//graphic actions
		for(int i = 0; i < gv.graphicList.Count; i++){	
			if (gv.graphicList[i].GetComponent<GraphicClass>().moveMe()){
				gv.removeGraphic(i);
				i--;
			}
		}
		//shooting
		if(gv.globalBonus == 1){
			shootTimer += 2;
		}else{
			shootTimer += 1;
		}
		if (Input.GetMouseButton(0) && shootTimer >= shootCooldown)  // 0- left click, 1- right, 2- middle 
        {
			shootTimer = 0;
			OTSprite nBullet = (Instantiate(bullet.gameObject) as GameObject).GetComponent<OTSprite>();
	        nBullet.position = gun.position + (gun.yVector * (gun.size.y / 2));
	        nBullet.rotation = gun.rotation;
			bulletClass bc = nBullet.GetComponent<bulletClass>();
			if(gv.gunState == 1){
				bc.explosionRange = 2.0f;
				bc.explosionPower = 0.4f;
			}else if(gv.gunState == 2){
				bc.explosionRange = 2.5f;
				bc.explosionPower = 0.5f;
			}else if(gv.gunState == 3){
				bc.explosionRange = 3.0f;
				bc.explosionPower = 0.6f;
				bc.explodeOnKillDamage = 1.5f;
				bc.explodeOnKillRange = 5.0f;
			}else if(gv.gunState == 4){
				bc.penitration = 1;
				bc.penitrationDamageLost = 0.4f;
			}else if(gv.gunState == 5){
				bc.penitration = 3;
				bc.penitrationDamageLost = 0.2f;
			}else if(gv.gunState == 6){
				bc.penitration = 999;
				bc.penitrationDamageLost = 0;
				bc.moveSpeed = 1.0f;
			}else if(gv.gunState == 7){
				bc.slowPower = 0.9f;
			}else if(gv.gunState == 8){
				bc.slowPower = 0.8f;
			}else if(gv.gunState == 9){
				bc.slowPower = 0.7f;
			}else if(gv.gunState == 10){
				bc.parts = 3;
				bc.partsDamage = 1;
				bc.explosionRange = 2.0f;
				bc.explosionPower = 0.4f;
			}else if(gv.gunState == 11){
				bc.parts = 3;
				bc.partsDamage = 1;
				bc.explosionRange = 2.5f;
				bc.explosionPower = 0.6f;
			}else if(gv.gunState == 12){
				bc.parts = 4;
				bc.partsDamage = 2;
				bc.explosionRange = 2.0f;
				bc.explosionPower = 0.4f;
			}else if(gv.gunState == 13){
				bc.slowPower = 0.9f;
				bc.explosionRange = 2.0f;
				bc.explosionPower = 0.4f;
			}else if(gv.gunState == 14){
				bc.slowPower = 0.8f;
				bc.explosionRange = 2.0f;
				bc.explosionPower = 0.4f;
			}else if(gv.gunState == 15){
				bc.slowPower = 0.9f;
				bc.explosionRange = 2.5f;
				bc.explosionPower = 0.5f;
			}else if(gv.gunState == 16){
				bc.penitration = 1;
				bc.penitrationDamageLost = 0.4f;
				bc.slowPower = 0.9f;
			}else if(gv.gunState == 17){
				bc.penitration = 1;
				bc.penitrationDamageLost = 0.4f;
				bc.slowPower = 0.8f;
			}else if(gv.gunState == 18){
				bc.penitration = 3;
				bc.penitrationDamageLost = 0.2f;
				bc.slowPower = 0.9f;
			}else if(gv.gunState == 19){
				bc.slowPower = 0.9f;
				bc.explosionRange = 2.0f;
				bc.explosionPower = 0.4f;
				bc.parts = 3;
				bc.partsDamage = 1;
			}
			
			gv.bulletList.Add(nBullet);
        }
	}
}
